package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomInteger;
import br.com.telescope.adsrt.dom.DomSourceCode;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomTextLong;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.EntityBinding;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.MultiRowInsertFeature;
import br.com.telescope.t2.feature.MultiRowUpdateFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Vazão de ensaio.
 * <p>
 * Interface para manutenção de Ponto utilizado no plano de execução
 * <P>
 * Telescope origin: ZTEST.CAD_PONTO_PLANO_EXECUCAO
 * <P>
 * Base entity: ZTEST.PONTO_PLANO_EXECUCAO
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadPontoPlanoExecucao extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "ZTEST.CAD_PONTO_PLANO_EXECUCAO";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Vazão de ensaio");
    }
    
    protected EntPontoPlanoExecucao base;
    protected EntFormulaExecucao fexc;
    protected EntPontoExecucao pexc;
    protected EntPontoPlanoExecucao rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pIdPlanoExecucao;
    
    // Record items
    private DomInteger aSequencia;
    private DomInteger aQuantidadeIteracoesPonto;
    private DomId aPlanoExecucaoId;
    private DomId aPontoExecucaoAnteriorId;
    private DomId aPontoExecucaoId;
    private DomId aFormulaCalcularVazaoId;
    private DomId aId;
    private DomUpper aFexcNome;
    private DomSourceCode aFexcFormula;
    private DomTextLong aFexcDescricao;
    private DomText aPexcSigla;
    private DomTextLong aPexcDescricao;
    
    // Query items
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " , FEXC.NOME as FEXC_NOME"
                + " , FEXC.FORMULA as FEXC_FORMULA"
                + " , FEXC.DESCRICAO as FEXC_DESCRICAO"
                + " , PEXC.SIGLA as PEXC_SIGLA"
                + " , PEXC.DESCRICAO as PEXC_DESCRICAO"
                + " , FEXC.ID as Z_FEXC_ID"
                + " , PEXC.ID as Z_PEXC_ID"
                + " from PONTO_PLANO_EXECUCAO BASE"
                + " left join FORMULA_EXECUCAO FEXC on FEXC.ID = BASE.FORMULA_CALCULAR_VAZAO_ID"
                + " join PONTO_EXECUCAO PEXC on PEXC.ID = BASE.PONTO_EXECUCAO_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aSequencia = new DomInteger();
        aSequencia.context(ctx).name("SEQUENCIA").label("Sequência");
        
        aQuantidadeIteracoesPonto = new DomInteger();
        aQuantidadeIteracoesPonto.context(ctx).name("QUANTIDADE_ITERACOES_PONTO").label("Quantidade de iterações neste ponto");
        aQuantidadeIteracoesPonto.setDefaultValue("1");
        
        aPlanoExecucaoId = new DomId();
        aPlanoExecucaoId.context(ctx).name("PLANO_EXECUCAO_ID").label("Plano de execução");
        
        aPontoExecucaoAnteriorId = new DomId();
        aPontoExecucaoAnteriorId.context(ctx).name("PONTO_EXECUCAO_ANTERIOR_ID").label("Ponto de execução anterior");
        
        aPontoExecucaoId = new DomId();
        aPontoExecucaoId.context(ctx).name("PONTO_EXECUCAO_ID").label("Ponto de execução");
        
        aFormulaCalcularVazaoId = new DomId();
        aFormulaCalcularVazaoId.context(ctx).name("FORMULA_CALCULAR_VAZAO_ID").label("Fórmula para calcular a vazão");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aFexcNome = new DomUpper();
        aFexcNome.context(ctx).name("FEXC_NOME").label("Nome da fórmula");
        aFexcNome.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT NOME as valor"
                + " , NOME as pesquisar"
                + " , NOME as descricao"
                + " from FORMULA_EXECUCAO FEXC)", "q"));
        aFexcNome.setAutocompleteLength(1);
        aFexcNome.setListOfValuesService("ZTEST.CAD_FORMULA_EXECUCAO");
        
        aFexcFormula = new DomSourceCode();
        aFexcFormula.context(ctx).name("FEXC_FORMULA").label("Fórmula (expressão) ");
        aFexcFormula.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT FORMULA as valor"
                + " , FORMULA as pesquisar"
                + " , FORMULA as descricao"
                + " from FORMULA_EXECUCAO FEXC)", "q"));
        aFexcFormula.setAutocompleteLength(1);
        
        aFexcDescricao = new DomTextLong();
        aFexcDescricao.context(ctx).name("FEXC_DESCRICAO").label("Descrição");
        aFexcDescricao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from FORMULA_EXECUCAO FEXC)", "q"));
        aFexcDescricao.setAutocompleteLength(1);
        
        aPexcSigla = new DomText();
        aPexcSigla.context(ctx).name("PEXC_SIGLA").label("Sigla do ponto de ensaio");
        aPexcSigla.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT SIGLA as valor"
                + " , SIGLA as pesquisar"
                + " , SIGLA as descricao"
                + " from PONTO_EXECUCAO PEXC)", "q"));
        aPexcSigla.setAutocompleteLength(2);
        
        aPexcDescricao = new DomTextLong();
        aPexcDescricao.context(ctx).name("PEXC_DESCRICAO").label("Descrição do ponto de ensaio");
        aPexcDescricao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from PONTO_EXECUCAO PEXC)", "q"));
        aPexcDescricao.setAutocompleteLength(2);
        
    }
    
    @Override
    public void initParameters() {
        pIdPlanoExecucao = new DomId(ctx, "ID_PLANO_EXECUCAO", "Plano de execução");
        pIdPlanoExecucao.setValue(ctx.getParameter("P_ID_PLANO_EXECUCAO"));
        pIdPlanoExecucao.setSqlExpression("PLANO_EXECUCAO_ID");
        if (pIdPlanoExecucao.isNotNull()) {
            aPlanoExecucaoId.setValue(pIdPlanoExecucao.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pIdPlanoExecucao);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntPontoPlanoExecucao record = getRecord();
        if (pIdPlanoExecucao.isNotNull()) {
            record.setPlanoExecucaoId(pIdPlanoExecucao.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pIdPlanoExecucao.setValue(getRecord().valueOfPlanoExecucaoId());
    }
    
    @Override
    public void initEntityBindings() {
        EntityBinding binding;
        
        binding = new EntityBinding("ZTEST.PONTO_EXECUCAO", "PEXC", "Z_PEXC_ID", "ZTEST.CAD_PONTO_EXECUCAO.VIEW");
        binding.addFields(aPexcSigla, aPexcDescricao);
        entityBindings.add(binding);
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntPontoPlanoExecucao getRecord() {
        if (base == null) {
            base = new EntPontoPlanoExecucao(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aSequencia.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "SEQUENCIA, PEXC_SIGLA, QUANTIDADE_ITERACOES_PONTO";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // PONTO_EXECUCAO_ID
        try {
            if (form.hasFields("PEXC_SIGLA")) {
                query = createQuery()
                        .select("PEXC.ID")
                        .from("PONTO_EXECUCAO", "PEXC")
                        .filter("PEXC.SIGLA", form.get("PEXC_SIGLA"))
                        ;
                base.setPontoExecucaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    @Override
    public void translateUpdateFks(Form form) {
        ModelQuery query;
        
        // PONTO_EXECUCAO_ID
        try {
            if (form.hasFields("PEXC_SIGLA")) {
                query = createQuery()
                        .select("PEXC.ID")
                        .from("PONTO_EXECUCAO", "PEXC")
                        .filter("PEXC.SIGLA", form.get("PEXC_SIGLA"))
                        ;
                base.setPontoExecucaoId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aSequencia, "view");
            addField(aPexcSigla, "view");
            addField(aQuantidadeIteracoesPonto, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aSequencia, "view");
            addField(aPexcSigla, "view");
            addField(aPexcDescricao, "view");
            addField(aQuantidadeIteracoesPonto, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends MultiRowInsertFeature {
        
        @Override
        public void initForm() {
            aSequencia.setOptional(false);
            addField(aSequencia, "rqd");
            aPexcSigla.setOptional(false);
            addField(aPexcSigla, "rqd");
            aPexcDescricao.setEditable(false);
            addField(aPexcDescricao, "view");
            aQuantidadeIteracoesPonto.setOptional(false);
            addField(aQuantidadeIteracoesPonto, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
            setRows(10);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends MultiRowUpdateFeature {
        
        @Override
        public void initForm() {
            aSequencia.setOptional(false);
            addField(aSequencia, "rqd");
            aPexcSigla.setOptional(false);
            addField(aPexcSigla, "rqd");
            aPexcDescricao.setEditable(false);
            addField(aPexcDescricao, "view");
            aQuantidadeIteracoesPonto.setOptional(false);
            addField(aQuantidadeIteracoesPonto, "rqd");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
            setRows(10);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aSequencia, "view");
            addField(aPexcSigla, "view");
            addField(aQuantidadeIteracoesPonto, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="PEXC_SIGLA_LOOKUP">
    public class FPexcSiglaLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aPexcSigla.setEditable(true);
            addField(aPexcSigla, "opt");
            aPexcDescricao.setEditable(false);
            addField(aPexcDescricao, "view");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("PEXC.SIGLA as PEXC_SIGLA")
                    .select("PEXC.DESCRICAO as PEXC_DESCRICAO")
                    .from("PONTO_EXECUCAO", "PEXC")
                    ;
        }
        
        {
            setFeatureName("PEXC_SIGLA_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FList());
        addFeature(new FView());
        addFeature(new FInsert());
        addFeature(new FUpdate());
        addFeature(new FDelete());
        addFeature(new FPexcSiglaLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "LIST";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public Integer getSequencia() {
        return aSequencia.getValue();
    }
    
    public void setSequencia(Integer value) {
        this.aSequencia.setValue(value);
    }
    
    public Integer getQuantidadeIteracoesPonto() {
        return aQuantidadeIteracoesPonto.getValue();
    }
    
    public void setQuantidadeIteracoesPonto(Integer value) {
        this.aQuantidadeIteracoesPonto.setValue(value);
    }
    
    public Long getPlanoExecucaoId() {
        return aPlanoExecucaoId.getValue();
    }
    
    public void setPlanoExecucaoId(Long value) {
        this.aPlanoExecucaoId.setValue(value);
    }
    
    public Long getPontoExecucaoAnteriorId() {
        return aPontoExecucaoAnteriorId.getValue();
    }
    
    public void setPontoExecucaoAnteriorId(Long value) {
        this.aPontoExecucaoAnteriorId.setValue(value);
    }
    
    public Long getPontoExecucaoId() {
        return aPontoExecucaoId.getValue();
    }
    
    public void setPontoExecucaoId(Long value) {
        this.aPontoExecucaoId.setValue(value);
    }
    
    public Long getFormulaCalcularVazaoId() {
        return aFormulaCalcularVazaoId.getValue();
    }
    
    public void setFormulaCalcularVazaoId(Long value) {
        this.aFormulaCalcularVazaoId.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public String getFexcNome() {
        return aFexcNome.getValue();
    }
    
    public void setFexcNome(String value) {
        this.aFexcNome.setValue(value);
    }
    
    public String getFexcFormula() {
        return aFexcFormula.getValue();
    }
    
    public void setFexcFormula(String value) {
        this.aFexcFormula.setValue(value);
    }
    
    public String getFexcDescricao() {
        return aFexcDescricao.getValue();
    }
    
    public void setFexcDescricao(String value) {
        this.aFexcDescricao.setValue(value);
    }
    
    public String getPexcSigla() {
        return aPexcSigla.getValue();
    }
    
    public void setPexcSigla(String value) {
        this.aPexcSigla.setValue(value);
    }
    
    public String getPexcDescricao() {
        return aPexcDescricao.getValue();
    }
    
    public void setPexcDescricao(String value) {
        this.aPexcDescricao.setValue(value);
    }
    
    public Long getParameterIdPlanoExecucao() {
        return pIdPlanoExecucao.getValue();
    }
    
    public void setParameterIdPlanoExecucao(Long value) {
        pIdPlanoExecucao.setValue(value);
        aPlanoExecucaoId.setValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/ztest/cad_ponto_plano_execucao.js";
    }
}
